<template>
  <div class="registerForm-box">
    <div class="registerForm-box1">
      <!-- 实验室logo -->
      <h1>
        <a href="javascript:;"><img src="@/assets/img/lab_logo.png" title="比特巅峰"/></a> <span>欢迎注册</span>
      </h1>
      <h4>已有账号？<a href="javascript:;">点击登录</a></h4>
      <!-- 表单 -->
      <section>
        <el-form :model="form" :rules="rules" ref="form" label-width="70px" class="form">
          <!-- 用户名 -->
          <el-form-item label="用 户 名">
            <el-input v-model="form.name" placeholder="请设置用户名" clearable></el-input>
          </el-form-item>
          <!-- 手机号 -->
          <el-form-item label="手 机 号" prop="phone">
            <el-input v-model="form.phone" placeholder="请输入手机号" clearable></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item label="密码" prop="pass" class="formPass" :class="{ show: show }">
            <label slot="label">密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码</label>
            <span class="mask">
              <div>长度为6~15个字符</div>
              <div>只允许出现字母、数字、下划线( _ ) 、@</div>
            </span>
            <el-input type="password" v-model="form.pass" @focus="isShow" placeholder="请设置密码" autocomplete="off" clearable show-password></el-input>
          </el-form-item>
          <!-- 确认密码 -->
          <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="form.checkPass" placeholder="请确认密码" autocomplete="off" clearable show-password></el-input>
          </el-form-item>
          <!-- 注册按钮 -->
          <input type="submit" class="primary" :class="{ disable: disable }" :disabled="disable" value="注 册" @click="onSubmit('form')" />
        </el-form>
      </section>
    </div>
  </div>
</template>

<script>
export default {
  name: 'RegisterForm',
  data() {
    // 手机号校验
    var validatePhone = (rule, value, callback) => {
      const regPhone = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
      if (value === '') {
        callback()
      } else {
        if (!regPhone.test(value)) {
          callback(new Error('手机号格式不正确'))
        } else callback()
      }
    }
    // 密码校验
    var validatePass = (rule, value, callback) => {
      //表单失去焦点时顺便让遮罩层隐藏
      this.show = false
      const regPass = /^[\w-@]{6,15}$/
      if (value === '') {
        callback(new Error('请输入密码'))
      } else if (!regPass.test(value)) {
        callback(new Error('密码设置不符合要求'))
      } else {
        if (this.form.checkPass !== '') {
          this.$refs.form.validateField('checkPass')
        }
        callback()
      }
    }
    // 确认密码校验
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback()
      } else if (this.form.pass === '') {
        this.$refs.form.validateField('pass')
      } else if (value !== this.form.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      // 控制密码处的遮罩层是否显示
      show: false,
      form: {
        // 用户名
        name: '',
        // 手机号
        phone: '',
        // 密码
        pass: '',
        // 确认密码
        checkPass: ''
      },
      rules: {
        // 手机号校验规则
        phone: [{ validator: validatePhone, trigger: 'blur' }],
        // 密码校验规则
        pass: [{ validator: validatePass, trigger: 'blur' }],
        // 确认密码校验规则
        checkPass: [{ validator: validatePass2, trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 提交
    onSubmit(formName) {
      this.$refs[formName].validate((valid, obj) => {
        if (valid) {
          console.log('submit!')
        } else {
          console.log(obj)
          return false
        }
      })
    },
    // 控制遮罩层显示
    isShow() {
      this.show = true
    }
  },
  computed: {
    // 控制注册按钮是否被禁用
    disable({ form }) {
      if (form.name !== '' && form.phone !== '' && form.pass !== '' && form.checkPass !== '') {
        return false
      }
      return true
    }
  }
}
</script>

<style lang="scss" scoped>
.registerForm-box {
  display: flex;
  /* 注册模块 */
  .registerForm-box1 {
    display: flex;
    flex-direction: column;
    padding: 35px 50px 0px 30px;
    box-sizing: border-box;
    width: 480px;
    background-color: rgba($color: #fff, $alpha: 0.9);
    border-radius: 10px;
    /* 欢迎注册标题 */
    h1 {
      position: relative;
      overflow: hidden;
      margin: 0 0 0 0;
      font-size: 36px;

      /* logo */
      a {
        img {
          position: absolute;
          left: 0px;
          top: 4px;
          width: 60px;
        }
      }
      span {
        margin-left: 65px;
      }
    }
    /* 点击登录 标题 */
    h4 {
      margin: 10px 0;
      color: #ccc;
      font-size: 14px;
      font-weight: normal;
      a {
        margin-left: 10px;
        text-decoration: none;
        color: rgba($color: #518dc4, $alpha: 0.8);
        &:hover {
          color: #518dc4;
        }
      }
    }
    /* 表单域 */
    .form {
      font-family: 'Microsoft Yahei';
      /* 设置输入框外边距 */
      .el-form-item {
        margin: 35px 0;
      }
      /* 清除表单轮廓默认样式(验证失败时) */
      /deep/ .el-form-item.is-error .el-input__inner {
        border-color: #dcdfe6;
      }
      /* 清除表单轮廓默认样式 */
      /deep/ .el-input__inner {
        border-color: #dcdfe6;
      }
      /* 密码框 */
      .formPass {
        position: relative;
        /* 密码框遮罩层 */
        .mask {
          position: absolute;
          z-index: 999;
          display: none;
          left: 15px;
          top: -80px;
          padding: 5px 10px 5px 15px;
          width: 290px;
          color: #fff;
          border-radius: 8px;
          border-bottom: none;
          font-size: 12px;
          background-color: rgba(#000000, $alpha: 0.8);
          /* 让遮罩层行高为文字的2.5倍 */
          div {
            line-height: 2.5;
          }
          /* 遮罩层的三角形 */
          &::after {
            position: absolute;
            right: 15px;
            bottom: -5px;
            content: '';
            transform: rotate(45deg);
            border: rgba($color: #000000, $alpha: 0.8) 6px solid;
            border-top-color: transparent;
            border-left-color: transparent;
          }
        }
      }
      //注册按钮样式
      .primary {
        margin: 10px 0 40px 0;
        padding: 12px 20px;
        width: 100%;
        box-sizing: border-box;
        box-shadow: 0 9px 21px 3px #ccc;
        white-space: nowrap;
        cursor: pointer;
        outline: 0;
        color: #fff;
        border: 1px solid transparent;
        background-color: rgba(#518dc4, 0.9);
        transition: all 0.3s;
        font-family: '宋体';
        font-weight: 600;
        font-size: 16px;
        border-radius: 30px;
        text-align: center;
        line-height: 1.5;
        &:hover {
          background-color: #518dc4;
        }
      }
      /* 注册按钮禁用时的样式 */
      .disable {
        cursor: no-drop;
        background-color: rgba(#518dc4, 0.4);
        box-shadow: 0 0px 0px 0 transparent;
        &:hover {
          background-color: rgba(#518dc4, 0.4);
        }
      }
    }
  }
  /* 控制密码框遮罩层显示 */
  .show {
    .mask {
      display: block !important;
    }
  }
}
  /* 屏幕尺寸<480px贴着顶部 */
  @media screen and (max-width: 480px) {
    .registerForm-box {
      padding-top: 0px;
      .registerForm-box1 {
        padding-top: 80px;
        padding-left: 20px;
        padding-right: 30px;
        border-radius: 0 0 5px 5px;
      }
    }
  }
/* 屏幕尺寸>=481px靠居中显示 */
@media screen and (min-width: 481px) {
  .registerForm-box {
    padding-top: 100px;
    justify-content: center;
    .registerForm-box1 {
      box-shadow: 10px 16px 25px 0px #100f0f78;
    }
  }
}
/* 屏幕尺寸>=768px靠右侧显示 */
@media screen and (min-width: 768px) {
  .registerForm-box {
    justify-content: flex-end;
    margin-right: 120px;
    padding-top: 100px;
  }
}
</style>
